/*
 * Package il.ac.biu.cs.grossmm.impl.presence
 * File PermanentEntity.java
 * 
 * This is a part of presence service framework prototype. 
 * See javadoc for more information.
 *  
 * Copyright (C) 2006 Grossmann Mark
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package il.ac.biu.cs.grossmm.impl.presence;

import java.util.Collections;
import java.util.Set;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import il.ac.biu.cs.grossmm.api.OperationFailedException;
import il.ac.biu.cs.grossmm.api.presence.Entity;
import il.ac.biu.cs.grossmm.api.presence.EntityType;

public class PermanentEntity implements Entity {
	final Integer id;
	final SqlRegistrar registrar;
	final Lock lock = new ReentrantLock();
	
	Set<String> identifiers;
	Set<Integer> roleIds;
	boolean isValid;
	
	long timeStamp;
	
	
	
	/**
	 * @param id
	 */
	public PermanentEntity(SqlRegistrar registrar, Integer id)
	{
		this.id = id;
		this.registrar = registrar;
	}


	public Set<String> getIdentifiers()
	{
		return identifiers;
	}

	public boolean is(EntityType entityType)
	{
		return roleIds.contains(entityType.getSerialId());
	}

	public final boolean isPermanent()
	{
		return true;
	}

	public boolean isValid()
	{
		return isValid;
	}


	@SuppressWarnings("unchecked")
	public <T> T getProperty(String property) throws OperationFailedException
	{
		return (T) registrar.getProperty(this, property);
	}

	@Override
	public boolean equals(Object obj)
	{
		if( obj instanceof PermanentEntity ) {
			PermanentEntity p = (PermanentEntity) obj;
			
			return p.id.equals(id);
		}
		
		return false;
	}
	
	@Override
	public int hashCode()
	{
		return id.hashCode();
	}
	
	@Override
	public String toString()
	{
		return "Entity " + id + " " + getIdentifiers();
	}
}
